/**
 * 
 */
package com.xbang.attendance.entity.stock;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.xbang.attendance.entity.product.Product;
import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.security.support.SecurityUserHolder;

import enumeration.InventoryOperation;

/**
 * @author dev.zhang
 * 
 *         仓储调货
 */
@Entity
public class InventoryExchange implements Serializable {

	private static final long serialVersionUID = -5028255031567029259L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	/** 原始仓库 */
	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, targetEntity = Warehouse.class)
	@JoinColumn(columnDefinition = "source_warehouse", referencedColumnName = "id")
	private Warehouse sourceWarehouse;

	/** 目标仓库 */
	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, targetEntity = Warehouse.class)
	@JoinColumn(columnDefinition = "indent_warehouse", referencedColumnName = "id")
	private Warehouse indentWarehouse;

	/** 产品 */
	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, targetEntity = Product.class)
	@JoinColumn(columnDefinition = "product_id", referencedColumnName = "id")
	private Product product;

	/** 调拨数量 */
	private BigDecimal amount;

	/**
	 * 状态
	 */
	@Enumerated(EnumType.STRING)
	private InventoryOperation invertoryOperation;

	/** 备注 */
	private String description;

	/** 操作人 */
	private String operationer;

	/** 操作日期 */
	private Date operationDate;

	/**
	 * 操作类型显示
	 * 
	 * @return
	 */
	public String getOperationDisplay() {
		return this.invertoryOperation.getAliseName();
	}

	public InventoryExchange updateOperation() {
		this.setOperationer(SecurityUserHolder.getCurrentUser().getName());
		this.operationDate = new Date();
		return this;
	}

	public InventoryExchange initial(Warehouse sourceWarehouse, Warehouse indentWarehouse, Product product, BigDecimal amount, String description,
			InventoryOperation inventOperation) {

		this.amount = amount;
		this.sourceWarehouse = sourceWarehouse;
		this.indentWarehouse = indentWarehouse;
		this.product = product;
		this.description = description;
		this.invertoryOperation = inventOperation;

		return updateOperation();
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the sourceWarehouse
	 */
	public Warehouse getSourceWarehouse() {
		return sourceWarehouse;
	}

	/**
	 * @param sourceWarehouse
	 *            the sourceWarehouse to set
	 */
	public void setSourceWarehouse(Warehouse sourceWarehouse) {
		this.sourceWarehouse = sourceWarehouse;
	}

	/**
	 * @return the indentWarehouse
	 */
	public Warehouse getIndentWarehouse() {
		return indentWarehouse;
	}

	/**
	 * @param indentWarehouse
	 *            the indentWarehouse to set
	 */
	public void setIndentWarehouse(Warehouse indentWarehouse) {
		this.indentWarehouse = indentWarehouse;
	}

	/**
	 * @return the product
	 */
	public Product getProduct() {
		return product;
	}

	/**
	 * @param product
	 *            the product to set
	 */
	public void setProduct(Product product) {
		this.product = product;
	}

	/**
	 * @return the amount
	 */
	public BigDecimal getAmount() {
		return amount;
	}

	/**
	 * @param amount
	 *            the amount to set
	 */
	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the operationer
	 */
	public String getOperationer() {
		return operationer;
	}

	/**
	 * @param operationer
	 *            the operationer to set
	 */
	public void setOperationer(String operationer) {
		this.operationer = operationer;
	}

	/**
	 * @return the operationDate
	 */
	public Date getOperationDate() {
		return operationDate;
	}

	/**
	 * @param operationDate
	 *            the operationDate to set
	 */
	public void setOperationDate(Date operationDate) {
		this.operationDate = operationDate;
	}

	/**
	 * @return the invertoryOperation
	 */
	public InventoryOperation getInvertoryOperation() {
		return invertoryOperation;
	}

	/**
	 * @param invertoryOperation
	 *            the invertoryOperation to set
	 */
	public void setInvertoryOperation(InventoryOperation invertoryOperation) {
		this.invertoryOperation = invertoryOperation;
	}

}
